package util;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertiesUtil {
	private static Properties prop = null;

	private static String config_file = "/config.properties";
	
	public static Map loadPropertiesFile() {
		return loadPropertiesFile(config_file);
	}
	
	public static Map loadPropertiesFile(String path) {
		Map map = new HashMap();
		if(prop == null){ 
			prop = new Properties();
			//FileInputStream fin = null;
			InputStream in = null;
			try {
				//fin = new FileInputStream(path);// path为 src/config.properties
				//prop.load(fin);
				in = PropertiesUtil.class.getResourceAsStream(path);// path为 /config.properties
				prop.load(in);
			} catch (FileNotFoundException e3) {
				// TODO Auto-generated catch block
				e3.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					//fin.close();
					if(in!=null) in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		map.putAll(prop);
		return map;
	}

	public static void savePropertiesFile(Map map, String path) {
		Properties prop = new Properties();
		prop.putAll(map);
		
		FileOutputStream fos = null;
		try {	
			fos = new FileOutputStream(path);
			prop.store(fos, path);

		}catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}
